package com.yushutao.menu.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.yushutao.menu.beans.UserBean;
import com.yushutao.menu.dao.IUser;
import com.yushutao.menu.dao.MyBatisUtil;

public class UserDAO implements IUser{

	@Override
	public void addUser(UserBean user) {
		try(SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()){
			IUser mapper = session.getMapper(IUser.class);
			mapper.addUser(user);
			session.commit();
		}
	}

	@Override
	public void payUser(int userId) {
		try(SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()){
			IUser mapper = session.getMapper(IUser.class);
			mapper.payUser(userId);
			session.commit();
		}
	}
	
	@Override
	public void removeUserById(int id) {
		try(SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()){
			IUser mapper = session.getMapper(IUser.class);
			mapper.removeUserById(id);
			session.commit();
		}
	}

	@Override
	public UserBean getUserById(int id) {
		try(SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()){
			IUser mapper = session.getMapper(IUser.class);
			return mapper.getUserById(id);
		}
	}

	@Override
	public UserBean getUserByPhoneAndDesk(String phoneNumber, String deskNumber) {
		try(SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()){
			IUser mapper = session.getMapper(IUser.class);
			return mapper.getUserByPhoneAndDesk(phoneNumber, deskNumber);
		}
	}

	@Override
	public List<UserBean> getUserReadyToPay() {
		try(SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()){
			IUser mapper = session.getMapper(IUser.class);
			return mapper.getUserReadyToPay();
		}
	}

}
